package com.mzba.place.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
/**
 * 
 * @author 06peng
 *
 */
public class PlatformTable {

	public String TABLE_NAME = "platform_db";
	public static final String _id = "_id";
	public static final String platform_key = "key";
	public static final String access_token = "access_token";
	public static final String expires_in = "expires_in";
	public static final String openid = "openid";
	/**
	 * 0新浪
	 * 1qq
	 * 2微信联系人
	 * 3微信朋友圈
	 * 4腾讯微博
	 */
	public static final String platform_type = "type";
	
	
	private DBManagerImpl db = null;
	
	public PlatformTable(Context context) {
		if (db == null) {
			db = DBManager.get(context);
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	public PlatformTable() {
		
	}
	
	public void createTable() {
		String createTableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ "(_id integer primary key autoincrement, " + access_token + " VARCHAR,"  + platform_key + "  VARCHARR," +
						" "+expires_in+" VARCHAR, "+openid+" VARCHAR, "+platform_type+" INTEGER)";
		db.creatTable(db.getConnection(), createTableSql, TABLE_NAME);
		
	}
	
	public void save(String key, String accessToken, long expiresIn, String openId, String type) {
		try {
			ContentValues values = new ContentValues();
			values.put(platform_key, key);
			values.put(access_token, accessToken);	
			values.put(expires_in, expiresIn);	
			values.put(openid, openId);
			values.put(platform_type, type);
			db.save(db, TABLE_NAME, values);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Cursor get(int type) {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME + " where "+platform_type+" = ?", new String[] {String.valueOf(type)});
		return cursor;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_NAME, null, null);
	}
}
